#include "mysql_save.h"
#include "mysql_helper.h"
#include <nel/misc/mutex.h>
#include <nel/misc/command.h>
#include <game_share/buf_fifo_ring.h>

namespace MSH
{

static NLMISC::CBufFIFORing<std::string>    _SQLString;
static CConnection		_MyDB;

void SaveThread::run ()
{
	_run = true;
	std::string* s = NULL;
	while( _run || (s = _SQLString.pop_front()) )
	{
		if( s != NULL )
		{
			_MyDB.query(*s);
			NLMISC::nlSleep(0);
		}
		else
		{
			NLMISC::nlSleep(10);
		}
	}
}

bool CMysqlSave::init( const std::string &hostName, const std::string &userName, const std::string &password, const std::string &defaultDatabase)
{
	_poolSize = 1024*1024*10;
	_MyDB.connect( hostName, userName, password, defaultDatabase );
	_thread = NLMISC::IThread::create (&_saveThread);
	_thread->start ();
	return true;
}

bool CMysqlSave::push( char* sql )
{
	std::string* s = new std::string(sql);
	if( s != NULL )
	{
		_SQLString.push_back(s);
		return true;
	}
	return false;
}





}


